package com.liangtian.mdm.system.group.service.impl;

import com.liangtian.mdm.dm.config.model.AttrConfig;
import com.liangtian.mdm.dm.listener.CacheUtil;
import com.liangtian.mdm.util.StringUtil;
import com.liangtian.mdm.system.group.dao.SysGroupDao;
import com.liangtian.mdm.system.group.model.SysGroup;
import com.liangtian.mdm.system.group.service.SysGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysGroupServiceImpl implements SysGroupService {

    @Autowired
    SysGroupDao sysGroupDao;

    @Autowired
    CacheUtil cacheUtil;

    @Override
    public Map<String, Object> queryGroupBySurveyType(Map<String, Object> param) {
        Map<String, Object> resMap = new HashMap<>();
        List<SysGroup> sysGroupList;
        String source = (String) param.get("isUsed");
        if ("1".equals(source)) {
            sysGroupList = sysGroupDao.queryGroupBySurveyType(param);
        } else {
            sysGroupList = sysGroupDao.queryGroupBySurveyTypeCreate(param);
        }

        resMap.put("allGroup", this.filterColumn(sysGroupList));

        List<String> perGroupList = sysGroupDao.queryPermissionGroupBySurveyType(param);
        List<String> groupIdLst = new ArrayList<String>();
        for (int i = 0; i < perGroupList.size(); i++) {
            groupIdLst.add(perGroupList.get(i));
        }
        resMap.put("permissionGroup", groupIdLst);
        return resMap;
    }

    private List<SysGroup> filterColumn(List<SysGroup> sysGroupList) {
        Map<String, String> map = new HashMap<String, String>();
        List<SysGroup> groupList = new ArrayList<SysGroup>();
        for (SysGroup group : sysGroupList) {
            if (null == map.get(group.getGroup_name())) {
                map.put(group.getGroup_name(), group.getAttr_colum());//不重复的属性组的名称
            } else {
                map.put(group.getGroup_name(), map.get(group.getGroup_name()) + "," + group.getAttr_colum());
            }
        }

        for (String key : map.keySet()) {
            for (SysGroup group : sysGroupList) {
                if (group.getGroup_name().equals(key)) {
                    group.setAttr_columStr(map.get(key));
                    groupList.add(group);
                    break;
                }
            }
        }
        return groupList;
    }

    @Override
    public void save(String surveyType, String userId, String uGroupId, String[] groupIds, String source) {
        Map<String, String> map = new HashMap<String, String>();
        List<AttrConfig> attrConfigLst;
        if (!StringUtil.isBlank(userId) && !("null".equals(userId)))
            map.put("userId", userId);
        if (!StringUtil.isBlank(uGroupId) && !("null".equals(uGroupId)))
            map.put("uGroupId", uGroupId);

        map.put("surveyType", surveyType);
        sysGroupDao.delGroupPerBySurvey(map); //保存属性组权限之前把这个用户，这个业务类型下的全删掉
        sysGroupDao.delAttrPerBySurvey(map); //保存属性权限之前把这个用户，这个业务类型下的全删掉
        for (String groupId : groupIds) {
            map.put("groupId", groupId);
            map.put("permissionCode", surveyType + ":" + groupId);
            sysGroupDao.insertGroupPer(map);  //保存属性组权限
            if ("1".equals(source)) {
                if ("0".equals(groupId)) {
                    groupId = null;
                }
                attrConfigLst = cacheUtil.getAttrListByGroup(surveyType.substring(surveyType.indexOf("_") + 1, surveyType.length()), groupId);
                for (AttrConfig conf : attrConfigLst) {
                    map.put("attrId", conf.getAttrId());
                    map.put("attrColumn", conf.getAttrColumn());
                    map.put("permissionCode", surveyType + ":" + conf.getAttrColumn().toLowerCase());
                    sysGroupDao.insertAttrPer(map); //保存属性权限
                }
            } else {
                List<String> attrColumnLst = sysGroupDao.queryColumnByGroupId(groupId);
                for (String str : attrColumnLst) {
                    map.put("attrId", "");
                    map.put("attrColumn", str);
                    map.put("permissionCode", surveyType + ":" + str.toLowerCase());
                    sysGroupDao.insertAttrPer(map); //保存属性权限
                }
            }

        }
    }
}
